package basic.com.liz.controller.business;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.WebRequest;

import com.ysten.jms.domain.JmsMessage;

import basic.com.liz.common.utils.DateProvider;
import basic.com.liz.common.utils.HttpResult;
import basic.com.liz.common.utils.HttpStatus;
import basic.com.liz.common.utils.SystemConfigLoader;
import basic.com.liz.common.utils.jms.JmsHelper;
import basic.com.liz.service.system.AccountService;
import basic.com.liz.service.system.ButtonService;
import basic.com.liz.service.system.RoleManageService;
import basic.com.liz.service.system.ShiroDbRealm.ShiroUser;
import basic.com.liz.service.system.SysMenuService;
import basic.com.liz.service.system.SystemConfigService;
import basic.com.liz.service.system.SystemDicService;
import basic.com.liz.service.system.UserService;

/**
 * <业务-基础Controller>
 * 
 * @author liwei
 * @version [版本号, 2015年7月16日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class BusinessBasicController
{
    
    @Autowired
    protected RoleManageService roleService;
    
    @Autowired
    protected SystemDicService systemDicService;
    
    @Autowired
    protected SystemConfigService systemConfigService;
    
    @Autowired
    protected SystemConfigLoader systemConfigLoader;
    
    @Autowired
    protected ButtonService buttonService;
    
    @Autowired
    protected SysMenuService sysMenuService;
    
    @Autowired
    protected AccountService accountService;
    
    @Autowired
    protected UserService userService;
    
    @Autowired
    protected JmsHelper jmsHelper;
    
    protected DateProvider dateProvider = DateProvider.DEFAULT;
    
    protected SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    
    protected SimpleDateFormat dfPart = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    
    protected SimpleDateFormat dfFull = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /**
     * 实现前台日期类型的转换
     * 
     * @param binder
     * @param request
     */
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
        
        /**
         * 自动转换日期类型的字段格
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
    }
    
    /**
     * 取出Shiro中的当前用户.
     */
    public ShiroUser getCurrentUser()
    {
        ShiroUser user = (ShiroUser)SecurityUtils.getSubject().getPrincipal();
        return user;
    }
    
    public HttpResult getCorrectResult()
    {
        HttpResult result = new HttpResult();
        result.setResult(HttpStatus.OK);
        return result;
    }
    
    public HttpResult getErrorResult()
    {
        HttpResult result = new HttpResult();
        result.setResult("300", "操作失败");
        return result;
    }
    
    public void doSendJms(List<Long> idsList, String target, String action, String topicName)
    {
        JmsMessage msg = null;
        for (Long dataId : idsList)
        {
            msg = new JmsMessage();
            msg.setAction(action);
            msg.setTarget(target);
            msg.setId(String.valueOf(dataId));
            jmsHelper.sendMessage(msg, topicName);
        }
        
    }
}
